package com.lyh.hkcloud.util.base;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lyh.hkcloud.R;

/**
 * Created by Spark
 */

public class LoadingUtils {

    private static LoadingUtils mInstance;
    private ViewGroup container;
    private LinearLayout loadingContainer;
    private static Context mContext;
    private Handler mHandler = new Handler();

    private LoadingUtils(Activity context) {
        mContext = context;
        container = context
                .findViewById(android.R.id.content);
        View mView = context.getLayoutInflater().inflate(
                R.layout.loading_layout, container);
        loadingContainer = mView.findViewById(R.id.loadingContainer);
    }

    public static LoadingUtils getInstance(Activity context) {
        if (mInstance == null) {
            mInstance = new LoadingUtils(context);
        }else if (!mContext.getClass().getName().endsWith(context.getClass().getName())) {
            mInstance = new LoadingUtils(context);
        }
        return mInstance;
    }

    public void show() {
        if (loadingContainer.getVisibility() == View.VISIBLE) {
            return;
        }
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadingContainer.setVisibility(View.VISIBLE);
            }
        }, 0);
    }


    public void hide() {
        if (loadingContainer.getVisibility() == View.VISIBLE) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    loadingContainer.setVisibility(View.GONE);
                }
            }, 0);
        }else{
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    loadingContainer.setVisibility(View.GONE);
                }
            }, 10);
        }
    }

    /**
     * 此方法主要为了解决用户在重启页面后单例还会持有上一个context，
     * 且上面的mContext.getClass().getName()其实是一样的
     * 所以使用上还需要在BaseActivity的onDestroy()方法中调用
     */
    public static void reset() {
        mInstance = null;
    }

}
